﻿using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace KoOLAP_TestWinForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //tabControl1.SelectedIndex = 1;
        }

        #region App Settings
        static public string GetAppSetting(String mName)
        {
            String vStr = null;
            
            string strPath =
@"C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strPath);
            XmlNode node = xmlDoc.SelectSingleNode(@"/configuration/appSettings");
            foreach (XmlNode keynode in node.ChildNodes)
            {
                string keyStr = keynode.Attributes["key"].Value;
                if (keyStr == mName)
                {
                    vStr = keynode.Attributes["value"].Value;
                    break;
                }
            }

            if (String.IsNullOrEmpty(vStr))
                vStr = "";
            return vStr;
        }
        #endregion App Settings

        private void btnPlan_Get_01_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            PlanProgress.Get(ref tbl, connStr, 2011, 11);
            gvPlan.DataSource = tbl;
        }

        private void btnPlan_Get_02_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            PlanProgress.Get2(ref tbl, connStr, 2011, 11);
            gvPlan.DataSource = tbl;
        }

        private void btnPlan_Get_03_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            PlanProgress.Get3(ref tbl, connStr, 2011, 11);
            gvPlan.DataSource = tbl;
        }

        private void btnPlan_Get_04_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            PlanProgress.Get4(ref tbl, connStr, "Александров", 2011, 11);
            gvPlan.DataSource = tbl;
        }

        private void btnPlan_Test_Click(object sender, EventArgs e)
        {
            //KoAD ad = new KoAD();
            //DataTable tbl = null;
            //ad.GetUsersForGroup(ref tbl, "groupname");
            //gvPlan.DataSource = tbl;

            //// uid
            //bool IsExist = false;
            //ad.IsExistUserInGroup(ref IsExist, "loginname", "groupname");

        }

        private void btnBIMainPanel_Get1_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            BI_SQL_MainPanel.GetPlanResume(ref tbl, connStr, 2011, 12);
            gvMainPanel.DataSource = tbl;
        }

        private void btnBIMainPanel_Get2_Click(object sender, EventArgs e)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Headers["Cache-Control"] = "no-cache";
            wc.UseDefaultCredentials = false;
            wc.Credentials = new System.Net.NetworkCredential("anonymous", "janeDoe@contoso.com");
            wc.UseDefaultCredentials = false;
            byte[] data = wc.DownloadData(@"http://sp.korablik.local:88/ws/BI/MainPanel.aspx");
            DataTable newTable = new DataTable("tbl");
            using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
            {
                xmlStream.Write(data, 0, data.Length);
                xmlStream.Position = 0;
                newTable.ReadXml(xmlStream);
                gvMainPanel.DataSource = newTable;
            }

        }

        private void btnBIMainPanel_Get3_Click(object sender, EventArgs e)
        {
            string connStr = GetAppSetting(@"BIConnect");
            DataTable tbl = null;
            new BI_SQL_CashAtYear().Get(ref tbl, connStr, 2011, 12);
            gvMainPanel.DataSource = tbl;
        }

        

    }

}
